import {
	getAccessToken
} from '@/common/api.js';
import {mpConfig} from '@/common/config.js'
import {
	Base64
} from '@/common/base64.js'
let util = {

	//返回上一页并刷新
	toPrePage: function(data) {
		console.log("------toPrePage---------")
		var pages = getCurrentPages();
		var prePage = pages[pages.length - 2];
		prePage.$vm.fromBackPage(data)
		uni.navigateBack()
	},

	formatFileSize: function(value) {
		if (null == value || value == '') {
			return "0 B";
		}
		var unitArr = new Array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
		var index = 0;
		var srcsize = parseFloat(value);
		index = Math.floor(Math.log(srcsize) / Math.log(1024));
		var size = srcsize / Math.pow(1024, index);
		size = size.toFixed(2); //保留的小数位数
		return size + unitArr[index];
	},

	//监听input和textarea, 高度变化
	judgeHeight(me) {

		let scrollTop = 0;

		let TEXTAREA = document.getElementsByTagName("textarea")
		let INPUT = document.getElementsByTagName("input")
		let keyBordHeight = 200

		//监听键盘收齐
		uni.onKeyboardHeightChange(res => {
			console.log("键盘发生变化" + res)
		})

		TEXTAREA.forEach(item => {
			// item.onclick = function(e) {
			// 	me.scrollTop = this.getBoundingClientRect().top
			// 	me.scrollViewHeight = me.scrollViewHeight - me.scrollTop + 100
			// }
			//获取焦点
			item.onfocus = function(e) {
				me.scrollTop = this.getBoundingClientRect().top //获取输入框距离顶部的高度
				me.scrollViewHeight = me.windowHeight - me.scrollTop + 100 //改变scrollView高度，将输入框上推
				// me.scrollTop = this.getBoundingClientRect().top;
				// me.scrollViewHeight -= keyBordHeight; //减去键盘的高度
				console.log("----------------获取焦点------------------")
				console.log("输入框距离顶部高度" + me.scrollTop);
				console.log("改变滚动区域高度为" + me.scrollViewHeight);

			}
			//失去焦点
			item.onblur = function(e) {
				me.scrollViewHeight = me.windowHeight - 80
				console.log("---------------失去焦点--------------------")
				console.log("改变滚动区域高度为" + me.scrollViewHeight)
			}
		})
		INPUT.forEach(item => {
			item.onclick = function(e) {
				me.scrollTop = this.getBoundingClientRect().top
			}
		})
	},
	getMpAccessToken(time) {
		let nowTime = new Date().getTime()
		let beforeTime = uni.getStorageSync("getAccessTokenTime")
	
		if (beforeTime + 7000 * 1000 < nowTime || !uni.getStorageSync("getAccessToken")) {
			let params = {
				grant_type: 'client_credential',
				appid: mpConfig.appId,
				secret: Base64.decode(mpConfig.appSecret)
			}
			return getAccessToken(params).then(res => {
				uni.setStorageSync("getAccessTokenTime", nowTime)
				uni.setStorageSync("getAccessToken", res.access_token)
				return res.access_token
			})
		} else {
			return Promise.resolve(uni.getStorageSync("getAccessToken"))
		}

	}

}
export {
	util
}
